package com.situ.skymall.admin.api.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.situ.admin.model.Admin;
import com.situ.admin.service.AdminService;
import com.situ.skymall.admin.api.mapper.AdminMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;


/**
 * 用户服务实现类
 */
@Service
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin> implements AdminService {

    private AdminMapper adminMapper;

    @Autowired
    public void setAdminMapper(AdminMapper adminMapper) {
        this.adminMapper = adminMapper;
    }



    @Override
    public Page<Admin> findAll(int pageNum, int pageSize, Admin search) {
        Page<Admin> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<Admin> wrapper = new LambdaQueryWrapper<>();

        // 添加查询条件
        if (search != null) {
            if (StringUtils.hasText(search.getAdminname())) {
                wrapper.like(Admin::getAdminname, search.getAdminname());
            }
        }

        return adminMapper.selectPage(page, wrapper);
    }

    @Override
    public Admin findByAdminname(String adminname) {
        LambdaQueryWrapper<Admin> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Admin::getAdminname, adminname);
        return this.getOne(wrapper);
    }

    @Override
    public boolean updateStatus(Integer id, Integer status) {
        Admin admin = this.getById(id);
        if (admin != null ) { // 只有未被删除的用户才能更新状态
            admin.setStatus(status);
            return this.updateById(admin);
        }
        return false;
    }


    @Override
    public boolean updateRoleType(Integer id, Integer roleType) {
        Admin admin = this.getById(id);
        if (admin != null) { // 只有未被删除的用户才能更新角色类型
            admin.setIsSuper(roleType);
            return this.updateById(admin);
        }
        return false;
    }

    @Override
    public boolean addAdmin(Admin admin) {
        return this.save(admin);
    }

    @Override
    public boolean deleteAdmin(Integer id) {
        Admin admin = this.getById(id);
        if (admin != null ) { // 只有未被删除的用户才能删除
            return this.updateById(admin);
        }
        return false;
    }
}